package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TrtcRecordTaskBo;
import org.dromara.system.domain.vo.TrtcRecordTaskVo;
import org.dromara.system.domain.TrtcRecordTask;
import org.dromara.system.mapper.TrtcRecordTaskMapper;
import org.dromara.system.service.ITrtcRecordTaskService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * trtc录制任务Service业务层处理
 *
 * @author aaron
 * @date 2025-04-25
 */
@RequiredArgsConstructor
@Service
public class TrtcRecordTaskServiceImpl implements ITrtcRecordTaskService {

    private final TrtcRecordTaskMapper baseMapper;

    /**
     * 查询trtc录制任务
     *
     * @param id 主键
     * @return trtc录制任务
     */
    @Override
    public TrtcRecordTaskVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询trtc录制任务列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return trtc录制任务分页列表
     */
    @Override
    public TableDataInfo<TrtcRecordTaskVo> queryPageList(TrtcRecordTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TrtcRecordTask> lqw = buildQueryWrapper(bo);
        Page<TrtcRecordTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的trtc录制任务列表
     *
     * @param bo 查询条件
     * @return trtc录制任务列表
     */
    @Override
    public List<TrtcRecordTaskVo> queryList(TrtcRecordTaskBo bo) {
        LambdaQueryWrapper<TrtcRecordTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TrtcRecordTask> buildQueryWrapper(TrtcRecordTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TrtcRecordTask> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(TrtcRecordTask::getRecordStartTime);
        lqw.eq(StringUtils.isNotBlank(bo.getRoomId()), TrtcRecordTask::getRoomId, bo.getRoomId());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestId()), TrtcRecordTask::getRequestId, bo.getRequestId());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), TrtcRecordTask::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskId()), TrtcRecordTask::getTaskId, bo.getTaskId());
        lqw.eq(bo.getRecordStartTime() != null, TrtcRecordTask::getRecordStartTime, bo.getRecordStartTime());
        lqw.eq(bo.getRecordEndTime() != null, TrtcRecordTask::getRecordEndTime, bo.getRecordEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoUrl()), TrtcRecordTask::getVideoUrl, bo.getVideoUrl());
        lqw.eq(bo.getSdkAppId() != null, TrtcRecordTask::getSdkAppId, bo.getSdkAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getFileId()), TrtcRecordTask::getFileId, bo.getFileId());
        return lqw;
    }

    /**
     * 新增trtc录制任务
     *
     * @param bo trtc录制任务
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TrtcRecordTaskBo bo) {
        TrtcRecordTask add = MapstructUtils.convert(bo, TrtcRecordTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改trtc录制任务
     *
     * @param bo trtc录制任务
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TrtcRecordTaskBo bo) {
        TrtcRecordTask update = MapstructUtils.convert(bo, TrtcRecordTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TrtcRecordTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除trtc录制任务信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public TrtcRecordTaskVo selectTaskInfoByRoomId(String roomId) {
        return baseMapper.selectTaskInfoByRoomId(roomId);
    }

    @Override
    public Long updateTaskInfo(TrtcRecordTaskVo trtcRecordTaskVo) {
        return baseMapper.updateTaskInfo(trtcRecordTaskVo);
    }
}
